package com.tj.collection.controller.lending.response;


import com.fqgj.common.api.ResponseData;
import com.fqgj.common.utils.DateUtil;
import com.tj.collection.db.entity.business.ClOrderAllocationEntity;
import com.tj.collection.utils.CalculatorUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: pavan
 * Date: 2019/9/10
 * Time: 下午5:15
 */
public class ClOrderHistoryResponse implements ResponseData {

    private List<Item> itemList = new ArrayList<>();

    public ClOrderHistoryResponse(List<ClOrderAllocationEntity> clOrderAllocationEntityList) {
        for (ClOrderAllocationEntity clOrderAllocationEntity : clOrderAllocationEntityList) {

            BigDecimal expireAmount = new BigDecimal(0);//应还总金额
            BigDecimal noRepayAmount = new BigDecimal(0);//待还款金额
            BigDecimal lateFee = new BigDecimal(0);//总罚息
            if (clOrderAllocationEntity.getOverdueDays() > 0) {
                BigDecimal overdueFee = CalculatorUtils.calculateOverdueFee(clOrderAllocationEntity.getBorrowCapital(),
                        clOrderAllocationEntity.getLateFeeRatio(),
                        clOrderAllocationEntity.getOverdueDays());
                lateFee = overdueFee.add(clOrderAllocationEntity.getLateServiceFee());
                expireAmount = CalculatorUtils.repaymentTotalCapital(clOrderAllocationEntity.getBorrowCapital(), clOrderAllocationEntity.getInterest(),
                        overdueFee, clOrderAllocationEntity.getLateServiceFee());
                noRepayAmount = expireAmount.subtract(clOrderAllocationEntity.getPaidAmount());
            } else {
                lateFee = new BigDecimal(0);
                expireAmount = CalculatorUtils.repaymentTotalCapital(clOrderAllocationEntity.getBorrowCapital(), clOrderAllocationEntity.getInterest(),
                        BigDecimal.ZERO, BigDecimal.ZERO);
                noRepayAmount = expireAmount.subtract(clOrderAllocationEntity.getPaidAmount());
            }
            expireAmount = expireAmount.setScale(2, BigDecimal.ROUND_HALF_UP);
            noRepayAmount = noRepayAmount.setScale(2, BigDecimal.ROUND_HALF_UP);
            lateFee = lateFee.setScale(2, BigDecimal.ROUND_HALF_UP);

            Item item = new Item()
                    .setTradeNo(clOrderAllocationEntity.getOrderNo())
                    .setName(clOrderAllocationEntity.getName())
                    .setMobile(clOrderAllocationEntity.getMobile())
                    .setIdentityNo(clOrderAllocationEntity.getIdentityNo())
                    .setRepaymentDate(DateUtil.getDate(clOrderAllocationEntity.getRepaymentDate()))
                    .setRealCapital(clOrderAllocationEntity.getBorrowCapital())
                    .setExpireAmount(expireAmount)
                    .setPaidAmount(clOrderAllocationEntity.getPaidAmount())
                    .setLateFee(lateFee)
                    .setFollowUp(clOrderAllocationEntity.getAgentName())
                    .setUserCode(clOrderAllocationEntity.getUserCode())
                    .setCollectionNo(clOrderAllocationEntity.getCollectionNo())
                    .setTradeNo(clOrderAllocationEntity.getOrderNo())
                    .setStageCode(clOrderAllocationEntity.getStageCode())
                    .setUserLanguage(clOrderAllocationEntity.getUserLanguage())
                    .setSourceMerchantCode(clOrderAllocationEntity.getSourceMerchantCode());
            this.itemList.add(item);
        }
    }

    private class Item {

        private String userCode;

        private String stageCode;

        private String collectionNo;

        private String tradeNo;

        private String name;

        private String mobile;

        private String identityNo;

        private String repaymentDate;

        private BigDecimal realCapital;

        private BigDecimal expireAmount;

        private BigDecimal paidAmount;

        private BigDecimal lateFee;

        private String followUp;

        /**
         * 语言
         */
        private String userLanguage;
        /**
         * 商户标识
         */
        private String sourceMerchantCode;

        public String getUserCode() {
            return userCode;
        }

        public Item setUserCode(String userCode) {
            this.userCode = userCode;
            return this;
        }

        public String getStageCode() {
            return stageCode;
        }

        public Item setStageCode(String stageCode) {
            this.stageCode = stageCode;
            return this;
        }

        public String getCollectionNo() {
            return collectionNo;
        }

        public Item setCollectionNo(String collectionNo) {
            this.collectionNo = collectionNo;
            return this;
        }

        public String getTradeNo() {
            return tradeNo;
        }

        public Item setTradeNo(String tradeNo) {
            this.tradeNo = tradeNo;
            return this;
        }

        public String getName() {
            return name;
        }

        public Item setName(String name) {
            this.name = name;
            return this;
        }

        public String getMobile() {
            return mobile;
        }

        public Item setMobile(String mobile) {
            this.mobile = mobile;
            return this;
        }

        public String getRepaymentDate() {
            return repaymentDate;
        }

        public Item setRepaymentDate(String repaymentDate) {
            this.repaymentDate = repaymentDate;
            return this;
        }

        public String getIdentityNo() {
            return identityNo;
        }

        public Item setIdentityNo(String identityNo) {
            this.identityNo = identityNo;
            return this;
        }

        public BigDecimal getRealCapital() {
            return realCapital;
        }

        public Item setRealCapital(BigDecimal realCapital) {
            this.realCapital = realCapital;
            return this;
        }

        public BigDecimal getExpireAmount() {
            return expireAmount;
        }

        public Item setExpireAmount(BigDecimal expireAmount) {
            this.expireAmount = expireAmount;
            return this;
        }

        public BigDecimal getPaidAmount() {
            return paidAmount;
        }

        public Item setPaidAmount(BigDecimal paidAmount) {
            this.paidAmount = paidAmount;
            return this;
        }

        public BigDecimal getLateFee() {
            return lateFee;
        }

        public Item setLateFee(BigDecimal lateFee) {
            this.lateFee = lateFee;
            return this;
        }

        public String getFollowUp() {
            return followUp;
        }

        public Item setFollowUp(String followUp) {
            this.followUp = followUp;
            return this;
        }

        public String getUserLanguage() {
            return userLanguage;
        }

        public Item setUserLanguage(String userLanguage) {
            this.userLanguage = userLanguage;
            return this;
        }

        public String getSourceMerchantCode() {
            return sourceMerchantCode;
        }

        public Item setSourceMerchantCode(String sourceMerchantCode) {
            this.sourceMerchantCode = sourceMerchantCode;
            return this;
        }

    }
}
